import java.util.logging.Logger
import org.semanticweb.owlapi.apibinding.OWLManager
import org.semanticweb.owlapi.model.*
import org.semanticweb.owlapi.reasoner.*
import org.semanticweb.owlapi.profiles.*
import org.semanticweb.owlapi.util.*
import org.mindswap.pellet.KnowledgeBase
import org.mindswap.pellet.expressivity.*
import org.mindswap.pellet.*
import org.semanticweb.owlapi.io.*
import org.semanticweb.elk.owlapi.*

def yeastpfile = new File("yeastphenotypes.txt")
def yeastgfile = new File("gene_association.sgd")

def onturi = "http://bioonto.de/pgkb.owl#"

OWLOntologyManager manager = OWLManager.createOWLOntologyManager()

OWLDataFactory fac = manager.getOWLDataFactory()
def factory = fac

OWLOntology ont = manager.loadOntologyFromOntologyDocument(new File("gene_ontology_ext.obo"))
def ontology = ont

def id2class = [:] // maps an OBO-ID to an OWLClass
ontology.getClassesInSignature(true).each {
  def a = it.toString()
  a = a.substring(a.indexOf("obo/")+4,a.length()-1)
  a = a.substring(a.indexOf('#')+1)
  a = a.replaceAll("_",":")
  a = a.replaceAll("tp://bio2rdf.org/","")
  if (id2class[a] == null) {
    id2class[a] = it
  }
  /* some DO specific stuff */
  if (a.startsWith("DOID") && (a.length()<12)) {
    def i = new Integer(a.substring(a.indexOf(":")+1)).toString()
    def ts = "DOID:"
    (7-i.length()).times { ts+="0" }
    ts += i
    id2class[ts] = it
  }
}

OWLReasonerFactory reasonerFactory = null

ConsoleProgressMonitor progressMonitor = new ConsoleProgressMonitor()
OWLReasonerConfiguration config = new SimpleConfiguration(progressMonitor)

OWLReasonerFactory f1 = new ElkReasonerFactory()
OWLReasoner reasoner = f1.createReasoner(ont,config)

reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY)

def processes = reasoner.getSubClasses(id2class["GO:0008150"], false).getFlattened()
def functions = reasoner.getSubClasses(id2class["GO:0003674"], false).getFlattened()
def cellparts = reasoner.getSubClasses(id2class["GO:0005575"], false).getFlattened()

def yp2go = [:]
yeastpfile.splitEachLine("\t") { line ->
  def id = line[0]
  def go = line[3]
  if (go!=null && go.length()>2 && go.startsWith("GO")) {
    if (yp2go[id] == null) {
      yp2go[id] = new TreeSet()
    }
    yp2go[id].add(go)
  }
}

def yg2go = [:]
yeastgfile.splitEachLine("\t") { line ->
  if (!line[0].startsWith("!")) {
    def id = line[1]
    def go = line[4]
    def ev = line[6]
    if (ev == "EXP" || ev == "IDA" || ev == "IPI" || ev == "IMP" || ev == "IGI" || ev == "IEP") {
    if (go!=null && go.length()>2 && go.startsWith("GO")) {
      if (yg2go[id] == null) {
	yg2go[id] = new TreeSet()
      }
      yg2go[id].add(go)
    }
    }
  }
}
def yg2goclosed = [:] // close against GO taxonomy
yg2go.keySet().each { sgd ->
  yg2goclosed[sgd] = new TreeSet()
  def gos = yg2go[sgd]
  gos.each { go ->
    yg2goclosed[sgd].add(go)
    if (id2class[go]!=null) {
      reasoner.getSuperClasses(id2class[go], false).getFlattened().each { 
	def a = it.toString()
	a = a.substring(a.indexOf("obo/")+4,a.length()-1)
	a = a.substring(a.indexOf('#')+1)
	a = a.replaceAll("_",":")
	if (a.startsWith("GO")) {
	  yg2goclosed[sgd].add(a)
	}
      }
    }
  }
}

def fout = new PrintWriter(new BufferedWriter(new FileWriter("yeast-new-go.txt")))
def match = 0
def mismatch = 0
yp2go.keySet().each { sgd ->
  def gop = yp2go[sgd]
  def gog = yg2goclosed[sgd]
  if (gog!=null && gog.size()>0) {
    gop.each { go ->
      //      if (id2class[go] in cellparts) {
	if (go in gog) {
	  match+=1
	} else {
	  mismatch+=1
	  fout.println("$sgd\t$go")
	}
	//      }
    }
  }
}
def total = match+mismatch
println "Total: "+total
println "Matches: $match ("+(match*100/total)+"%)"
println "Mismatches: $mismatch ("+(mismatch*100/total)+"%)"
fout.flush()
fout.close()
System.exit(0)
